<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class Follower extends CActiveRecord{
	const PAGE_SIZE=16;

	// const STATUS_FOLLOW=1;

	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public function tableName(){
		return 'user_followers';
	}	

	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->ctime=date('Y-m-d H:i:s');
				$this->uid=Yii::app()->user->id;
			}
			return true;
		}
		else
			return false;
	}
	//关注好友
	public static function follow($fid=false){
		$id = Yii::app()->user->id;
		if(User::model()->findByPk($fid)){//检测要关注的人是否存在
			$follow = self::getFollowStatus($fid);
			if($follow){
				if($follow->uid==$id){
					if($follow->status==2)$follow->status=3;
				}elseif($follow->fid==$id){
					if($follow->status==1)$follow->status=3;
				}
			}else{
				$follow = new Follower;
				$follow->uid = $id;
				$follow->fid = $fid;
			}

			if($follow->save()){
				$msg['status']=0;
				$msg['desc']='好友关注成功';
			}else{
				$msg['status']=1;
				$msg['desc']='好友关注失败';
			}

		}else{
			$msg['status']=1;
			$msg['desc']='您关注的人不存在';
		}

		return $msg;
	}
	//取消关注好友
	public static function refollow($fid=false){
		$id = Yii::app()->user->id;
		$status=0;
		if(User::model()->findByPk($fid)){//检测要关注的人是否存在
			$follow = self::getFollowStatus($fid);
			if($follow){
				if($follow->uid==$id){
					if($follow->status==3){
						$status=2;
						$follow->status = 2;
					}
				}elseif($follow->fid==$id){
					if($follow->status==3){
						$status=1;
						$follow->status = 1;
					}
				}
			$rs = $status?$follow->save():$follow->delete();

			if($rs){
				$msg['status']=0;
				$msg['desc']='取消关注成功';
			}else{
				$msg['status']=1;
				$msg['desc']='取消关注失败';
			}
			}else{
				$msg['status']=1;
				$msg['desc']='您并未关注此人';
			}
			

		}else{
			$msg['status']=1;
			$msg['desc']='您取消关注的人不存在';
		}

		return $msg;
	}

	//检测两人的好友状态
	public static function getFollowStatus($fid,$id=false){
		$id=$id?$id:Yii::app()->user->id;
		return Follower::model()->findByAttributes(array('uid'=>$id,'fid'=>$fid))?Follower::model()->findByAttributes(array('uid'=>$id,'fid'=>$fid)):Follower::model()->findByAttributes(array('uid'=>$fid,'fid'=>$id));
	}

	//检测id是否关注了fid
	public static function isFollow($fid,$id=false){
		$f=self::getFollowStatus($fid,$id);

		if(!$f)return false;
		elseif ($f->status==3) {
			return true;
		}
		else {
			if(($f->uid==$id&&$f->status==1)||($f->fid==$id&&$f->status==2))return true;
			else return false;
		}
	}

	public static function getList($status=3){
		$uid = Yii::app()->user->id;


		switch ($status) {
      	case 1://我关注的
				$condition .= " ( status=1 AND uid=:uid  ) OR ( status=2 AND fid=:uid ) ";
      	break;
      	case 2://关注我的
				$condition .= " ( status=2 AND uid=:uid ) OR ( status=1 AND fid=:uid ) ";
      	break;
      	default:
				$condition .= " status=3 AND (uid=:uid OR fid=:uid) ";
      	break;
      }
		$params['uid']=Yii::app()->user->id;
		$criteria=new CDbCriteria();
	

		$criteria->condition = $condition;
		$criteria->params = $params;
		$criteria->select = 'id,uid,fid,status';
		$criteria->order = 'id DESC';
    	$count=Follower::model()->count($criteria);
    	$pages=new CPagination($count);

    	// results per page
   	 	$pages->pageSize=self::PAGE_SIZE;
    	$pages->applyLimit($criteria);
    	$rs['total_num'] = $count;
    	$rs['rows'] = Follower::model()->findAll($criteria);
    	$rs['pages'] = $pages;
    	return $rs;
  }
}